using System;
using System.Collections;
using System.Xml;

using MediaWikiEngine.Domain;

namespace MediaWikiEngine.Command
{
	/// <summary>
	/// Summary description for GetPage.
	/// </summary>
	public class UpdateWatch : BaseCommand
	{
		public UpdateWatch(CommandParams param, WikiConnection conn) : base(param, conn)
		{
		}

        public UpdateWatch(WikiConnection conn, WikiPage pagedata) : base(conn)
        {
            CommandParams parameters = new CommandParams();
            parameters["pagedata"] = pagedata;

            _params = parameters;
        }

		/// <summary>
		/// Creates a Wiki Update Watch command using an Xml text writer.
		/// </summary>
		/// <param name="inXW"></param>
		/// <param name="inPagedata"></param>
		/// <param name="inTitle"></param>
		static public void ToXml(
			XmlTextWriter inXW,
			string inNamespace,
			string inTitle)
		{
			StartCommandToXml(inXW, "wiki:updatewatch");

			StartConditionsToXml(inXW);
			IfConditionTestToXml(inXW, "$loginsuccess", "equals", "True", "bot:exit");
			EndConditionsToXml(inXW);

			StartInputToXml(inXW);
			InputNameValueToXml(inXW, "pagedata", "$page");
			EndInputToXml(inXW);

			EndCommandToXml(inXW);
		}

		public override CommandResult Execute()
		{
			WikiPage page = (WikiPage) _params["pagedata"];

            if (null != page.Watch)
            {
                string action;
                if (true == page.Watch.Value)
                    action = "watch";
                else
                    action = "unwatch";

                _conn.DoGet(_conn.Endpoint.PageWithMode(page.Namespace, page.Title, action));
            }

			return new UpdateWatchResult(new Hashtable());
		}
	}

    public class UpdateWatchResult : CommandResult
    {
        public UpdateWatchResult(Hashtable results)
            : base(results)
        {
        }

    }
}
